<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Cache,Event,Auth,Excel,Image;
class UnitsController extends Controller
{

    public function messages($table){
        if($table == 'units'){
            return [
                'name.required' => '单位名不能为空',
                'address.required'  => '详细地址不能为空',
                'zuzhijigou.digits' => '社会信用码为18位数字',
                'type.required' => '行业类别不能为空',
                'is_imp.required' => '重点级别不能为空',
                'xingzhi.required' => '单位性质不能为空',
                'gdzc.required' => '固定资产不能为空',
                'zaizhi_num_of_peo.required' => '在职职工总数不能为空',
                'frdb_name.required' => '单位法人不能为空',
                'frdb_phone.regex' => '法人联系电话格式错误',
                'bwfzr_name.required' => '保卫负责人不能为空',
                'bwfzr_phone.regex' => '保卫负责人联系电话格式错误',
                'minjing.required' => '责任民警不能为空',
                'jd.required' => '经度不能为空',
                'wd.required' => '纬度不能为空',
            ];
        }
        if($table == 'faren'){
            return [
                'unit_name.required' => '单位名不能为空',
                'name.required'  => '姓名不能为空',
                'sex.required' => '性别不能为空',
                'birthday.required' => '出生年月不能为空',
                'nation.required' => '民族不能为空',
                'native_place.required' => '籍贯不能为空',
                'education.required' => '文化程度不能为空',
                'political_status.required' => '政治面貌不能为空',
                'id_number.regex' => '身份证号格式错误',
                'email.email' => '邮箱格式错误',
                'phone.regex' => '联系电话格式错误',
            ];
        }
        if($table == 'buwei'){
            return [
                'unit_name.required' => '单位名不能为空',
                'name.required'  => '部位名不能为空',
                'fuzeren.required' => '安全负责人不能为空',
                'dengji.required' => '部位等级不能为空',
                'type.required' => '要害类型不能为空',
                'aqff_01.required' => '请选择要害部位安全管理制度',
                'aqff_02.required' => '请选择消防安全管理制度',
                'aqff_03.required' => '请选择值守方式',
                'aqff_04.required' => '请选择人员、车辆出入登记',
                'aqff_05.required' => '请选择防护目标的门窗柜等物理设施',
                'aqff_06.required' => '请选择保安器材装备配置',
                'aqff_07.required' => '请选择视频监控系统',
                'aqff_08.required' => '请选择室内入侵报警系统',
                'aqff_09.required' => '请选择门禁系统',
                'aqff_10.required' => '请选择室外周界报警系统',
                'aqff_11.required' => '请选择疏散指示标志',
                'aqff_12.required' => '请选择应急照明灯具配备',
                'aqff_13.required' => '请选择消防设施及器材配备',
                'aqff_14.required' => '请选择灭火设备的操作',
            ];
        }
        if($table == 'yuan'){
            return [
                'unit_name.required' => '编制单位不能为空',
                'name.required'  => '预案主题不能为空',
                'bianzhiren.required' => '编制人不能为空',
                'bianzhi_time.required' => '编制时间不能为空',
                'content.required' => '预案内容不能为空',
            ];
        }
        if($table == 'type'){
            return [
                'name.required'  => '类型名称不能为空',
            ];
        }
    }

    public function index(request $request)
    {
        $user = Auth::guard('admin')->user();
        if($request->rows) {
            $map = array();
            if($user->dengji == 2){
                $map['units.city'] = $user->city;
            }
            if($user->dengji == 3){
                $map['units.area'] = $user->area;
            }
            if($user->dengji == 4){
                $map['units.org_id'] = $user->org_id;
                $map['units.minjing'] = $user->id;
            }
            //如果是企业用户
            if($user->role_id == 22){
                $map['units.id'] = $user->unit_id;
            }
            if($request->city != null){
                if($request->city == 0){

                }else{
                    $map['units.city'] = $request->city;
                }
            }
            if($request->area){
                $map['units.area'] = $request->area;
            }
            if($request->org_id){
                $map['units.org_id'] = $request->org_id;
            }
            if($request->name){
                $map[] = array('units.name','like','%'.$request->name.'%');
            }
            if($request->input('type') != null){
                $map['units.type'] = $request->type;
            }
            if($request->input('xingzhi') != null){
                $map['units.xingzhi'] = $request->xingzhi;
            }
            if($request->input('jibie') != null){
                $map['units.is_imp'] = $request->jibie;
            }
            if($request->input('is_del') != null){
                $map['units.is_del'] = $request->is_del;
            }
            $res = DB::table('units')
                ->where($map)
                ->leftJoin('area as a', 'units.city', '=', 'a.id')
                ->leftJoin('area as b', 'units.area', '=', 'b.id')
                ->leftJoin('area as c', 'units.org_id', '=', 'c.id')
                ->leftJoin('unit_type as d', 'units.type', '=', 'd.id')
                ->leftJoin('admin_users as e', 'units.user_id', '=', 'e.id')
                ->orderBy('units.city','asc')
                ->orderBy('units.area','asc')
                ->orderBy('units.org_id','asc')
                ->orderBy('units.is_imp','asc')
                ->orderBy('units.type','asc')
                ->orderBy('units.xingzhi','asc')
                ->select('units.id','units.is_imp','units.type','units.xingzhi','units.name','units.address','units.frdb_name','units.frdb_phone', 'a.name as city', 'b.name as area', 'c.name as org_id', 'd.name as type', 'e.name as user_id')
                ->paginate($request->rows);
            $list = $res->items();
            foreach ($list as $k=>$v){
                if($v->is_imp == 10){
                    $list[$k]->is_imp = '省级重点单位';
                }elseif($v->is_imp == 20){
                    $list[$k]->is_imp = '市级重点单位';
                }elseif($v->is_imp == 30){
                    $list[$k]->is_imp = '县区级重点单位';
                }elseif($v->is_imp == 40){
                    $list[$k]->is_imp = '一般单位';
                }

                if($v->xingzhi == 10){
                    $list[$k]->xingzhi = '国有';
                }elseif($v->xingzhi == 20){
                    $list[$k]->xingzhi = '民营';
                }elseif($v->xingzhi == 30){
                    $list[$k]->xingzhi = '股份';
                }elseif($v->xingzhi == 40){
                    $list[$k]->xingzhi = '合资';
                }elseif($v->xingzhi == 50){
                    $list[$k]->xingzhi = '外资';
                }elseif($v->xingzhi == 60){
                    $list[$k]->xingzhi = '个体';
                }
            }
            $result['total'] = $res->total();
            $result['rows'] = $list;
            echo json_encode($result);
        }else{
            //类型
            $type = db::table('unit_type')->where('is_del','0')->get();
            //市
            $city = db::table('area')->where('dengji','=','1')->orderby('id')->get();
            if($user->role_id == 22 && $user->unit_id){
                $status = db::table('units')->where('id',$user->unit_id)->value('status');
                return view('admin.units.index', ['type'=>$type,'city' => $city,'status'=>$status]);
            }
            //民警
            $police = db::table('admin_users')->where('org_id',$user->org_id)->where('role_id',15)->where('dengji','4')->get();
            //行业字段
            $field_count = db::table('unit_type_field')->where('type_id',$user->unit_type)->where('is_show','1')->count();
            if($field_count > 0){
                for ($x=0; $x*2<$field_count; $x++) {
                    $field[$x] = db::table('unit_type_field')->where('type_id',$user->unit_type)->where('is_show','1')->orderby('paixu','asc')->offset(2*$x)->limit(2)->get();
                }
                foreach ($field as $k=>$v){
                    foreach ($v as $k2=>$v2){
                        if($v2->field_type == 2){
                            $v2->daan = db::table('unit_type_field_daan')->where('pid',$v2->id)->get();
                        }
                    }
                }
            }else{
                $field = array();
            }
            return view('admin.units.index', ['type'=>$type,'city' => $city,'police'=>$police,'field'=>$field]);
        }
    }

    public function guanli($id){
        $re = DB::table('units')->where('id',$id)->first();
        return view('admin.units.guanli', ['re' => $re]);
    }

    public function jiben(request $request)
    {
        if(Auth::guard('admin')->user()->org_id != null){
            $map['org_id'] = Auth::guard('admin')->user()->org_id;
        }
        if($request->isMethod('post')){
            if(input::get('name')){
                $map[] = array('name','like','%'.input::get('name').'%');
            }
            if(input::get('org_id')){
                $map['org_id'] = input::get('org_id');
            }
            if(input::get('is_imp')){
                $map['is_imp'] = input::get('is_imp');
            }
        }
        if(Auth::guard('admin')->user()->id == 11){
            $map['id'] = '18';
        }

        $map['is_del'] = '0';
        $res = DB::table('units')->where($map)->orderby('id','desc')->paginate(10);
        $count = DB::table('units')->where($map)->count();

        return view('admin.units.jiben', ['res' => $res,'count'=>$count]);
    }

    public function view($id)
    {
        $re = DB::table('units')->where('id',$id)->first();
        return view('admin.units.view', ['re' => $re,'id'=>$id]);
    }

    public function mubiao(request $request)
    {
        if(Auth::guard('admin')->user()->org_id != null){
            $map['org_id'] = Auth::guard('admin')->user()->org_id;
        }
        if($request->isMethod('post')){
            if(input::get('name')){
                $map[] = array('name','like','%'.input::get('name').'%');
            }
            if(input::get('org_id')){
                $map['org_id'] = input::get('org_id');
            }
            if(input::get('is_imp')){
                $map['is_imp'] = input::get('is_imp');
            }
        }

        $map['is_del'] = '0';
        $res = DB::table('units')->where($map)->orderby('id','desc')->paginate(10);
        $count = DB::table('units')->where($map)->count();

        return view('admin.units.mubiao', ['res' => $res,'count'=>$count]);
    }

    public function important(request $request)
    {
        if(Auth::guard('admin')->user()->org_id != null && Auth::guard('admin')->user()->id != 1){
            $map['org_id'] = Auth::guard('admin')->user()->org_id;
        }
        if($request->isMethod('post')){
            if(input::get('name')){
                $map[] = array('name','like','%'.input::get('name').'%');
            }
            if(input::get('org_id')){
                $map['area_id'] = input::get('org_id');
            }
        }
        $map['is_del'] = '0';
        $map['is_imp'] = '1';
        $res = DB::table('units')->where($map)->paginate(10);
        $count = DB::table('units')->where($map)->count();
        return view('admin.units.important', ['res' => $res,'count'=>$count]);
    }
    
    public function add(Request $request)
    {
        if($request->isMethod('post')){
            $user = Auth::guard('admin')->user();
            $this->validate($request, [
                'name' => 'required',
                'address' => 'required',
                'zuzhijigou' =>'digits:18',
                'type' =>'required',
                'is_imp' =>'required',
                'xingzhi'=>'required',
                'gdzc'=>'required',
                'zaizhi_num_of_peo'=>'required',
                'frdb_name'=>'required',
                'frdb_phone'=>array('regex:/^((0\d{2,3}-\d{7,8})|(1[3584]\d{9}))$/'),
                'bwfzr_name'=>'required',
                'bwfzr_phone'=>array('regex:/^((0\d{2,3}-\d{7,8})|(1[3584]\d{9}))$/'),
                'minjing'=>'required',
                'jd' =>'required',
                'wd' =>'required',
            ],$this->messages('units'));
            if(!Input::get('type')){
                return ['status'=>0,'msg'=>'请选择行业类别'];
            }
            if(Input::get('city') && Input::get('area') && Input::get('org_id')){
                $data = Input::except('_token','field');
                if($request['pic']){
                    $filedir = "upload/qiye/".date('Y-m-d',time())."/";
                    if (!file_exists($filedir)){
                        mkdir ($filedir,0777,true);
                    }
                    $images = $request['pic'];
                    $extension = $images -> getClientOriginalExtension();
                    $newImagesName=md5(time()).random_int(5,5).".".$extension;
                    $thumb_path = $filedir.'thumb_'.$newImagesName;
                    Image::make($request['pic'])->resize(50, 50)->save($thumb_path);
                    $images->move($filedir,$newImagesName);
                    $data['pic'] = $filedir.$newImagesName;
                    $data['thumb_pic'] = $thumb_path;
                }else{
                    $data['pic'] = "images/moren.jpg";
                    $data['thumb_pic'] = "images/thumb_moren.jpg";
                }
                $data['user_id'] = Auth::guard('admin')->user()->id;
                $data['created_at'] = date('Y-m-d H:i:s',time());
                $data['updated_at'] = date('Y-m-d H:i:s',time());
                foreach ($data as $k=>$v){
                    $data[$k] = htmlspecialchars($v);
                }
                $res = DB::table('units')->insertGetId($data);
                //处理行业字段
                $field_data = Input::get('field');
                if($field_data){
                    foreach ($field_data as $k=>$v){
                        $array['unit_id'] = $where['unit_id'] = $res;
                        $array['type_id'] = $where['type_id'] = $user->unit_type;
                        $array['field_id'] = $where['field_id'] = db::table('unit_type_field')->where('type_id',$user->unit_type)->where('field_name',$k)->value('id');
                        $result = db::table('unit_type_field_value')->where($where)->first();
                        if($result){
                            $array['field_name'] = $k;
                            $array['val'] = $v;
                            foreach ($array as $k=>$v){
                                $array[$k] = htmlspecialchars($v);
                            }
                            db::table('unit_type_field_value')->where($where)->update($array);
                        }else{
                            $array['field_name'] = $k;
                            $array['val'] = $v;
                            foreach ($array as $k=>$v){
                                $array[$k] = htmlspecialchars($v);
                            }
                            db::table('unit_type_field_value')->insert($array);
                        }
                    }
                }

                if (Auth::guard('admin')->user()->unit_id == '') {
                    $s['id'] = Auth::guard('admin')->user()->id;
                    $sa['unit_id'] = $res;
                    DB::table('admin_users')->where($s)->update($sa);
                }

                return ['status'=>1,'msg'=>'添加成功'];
                //return redirect('admin/units/index/');
            }else{
                return ['status'=>0,'msg'=>'请选择辖区派出所'];
            }

        }
        //类型
        $type = db::table('unit_type')->where('is_del','0')->get();
        //市
        $city = db::table('area')->where('dengji','=','1')->orderby('id')->get();
        return view('admin.units.add', ['type'=>$type,'city' => $city]);
    }

    public function edit($id,Request $request)
    {
        $re = DB::table('units')->where('id',$id)->first();
        if($request->isMethod('post')){
            //一级审核
            if($re->status == 5){
                return ['status'=>0,'msg'=>'您已编辑过了，请点击上方更新按钮，等待后台审核'];
            }
            if($re->status == 10){
                return ['status'=>0,'msg'=>'您已提交更新，请等待后台审核'];
            }
            //二级审核
            /*if($re->status == 5 || $re->status == 10 || $re->status == 20){
                return ['status'=>0,'msg'=>'您已编辑过了，等待后台审核通过后才能编辑'];
            }*/
            $this->validate($request, [
                'name' => 'required',
                'address' => 'required',
                'zuzhijigou' =>'digits:18',
                'type' =>'required',
                'is_imp' =>'required',
                'jd' =>'required',
                'wd' =>'required',
            ],$this->messages('units'));
            $obj = DB::table('units');
            $data = Input::except('_token','field');
            if(!Input::get('type')){
                return ['status'=>0,'msg'=>'请选择行业类别'];
            }
            if($data['org_id']){
                $area = DB::table('area')->where('id','=',$data['org_id'])->value('cid');
                if($area != $data['area']){
                    return ['status'=>0,'msg'=>'系统错误，请重新操作'];
                }
            }else{
                $data['org_id'] = $re->org_id;
                $data['area'] = $re->area;
                $data['city'] = $re->city;
            }

            if($data['area']){
                $city = DB::table('area')->where('id','=',$data['area'])->value('cid');
                if($city != $data['city']){
                    return ['status'=>0,'msg'=>'系统错误，请重新操作'];
                }
            }else{
                $data['area'] = $re->area;
            }

            if(!$data['city']){
                $data['city'] = $re->city;
            }
            //print_r($data);
            //上传图片
            if($request['pic']){
                $filedir = "upload/qiye/".date('Y-m-d',time())."/";
                if (!file_exists($filedir)){
                    mkdir ($filedir,0777,true);
                }
                $images = $request['pic'];
                $extension = $images -> getClientOriginalExtension();
                $newImagesName=md5(time()).random_int(5,5).".".$extension;
                $thumb_path = $filedir.'thumb_'.$newImagesName;
                Image::make($request['pic'])->resize(50, 50)->save($thumb_path);
                $images->move($filedir,$newImagesName);
                $data['pic'] = $filedir.$newImagesName;
                $data['thumb_pic'] = $thumb_path;
            }
            $userinfo = auth::user();
            $i = 0;
            //统计修改的基本信息字段数量
            foreach ($data as $k=>$v){
                $data[$k] = htmlspecialchars($v);
                if($re->$k != $v){
                    $i++;
                }
            }

            //更新基本信息
            $obj->where('id',$id)->update($data);

            //统计修改的行业信息字段数量
            $field_data = Input::get('field');
            if($field_data){
                foreach ($field_data as $k=>$v){
                    $array['unit_id'] = $where['unit_id'] = $id;
                    $array['type_id'] = $where['type_id'] = $re->type;
                    $array['field_id'] = $where['field_id'] = db::table('unit_type_field')->where('type_id',$re->type)->where('field_name',$k)->value('id');
                    $result = db::table('unit_type_field_value')->where($where)->first();
                    if($result){
                        if($result->val != $v){
                            $i++;
                        }
                    }
                }
            }

            //如果有信息被修改了，添加一条修改记录，并将企业状态更改为已编辑待审核，等待app上审核通过之后才能再次编辑
            if($i > 0){
                $update['unit_id'] = $re->id;
                $update['field_num'] = $i;
                $update['user_id'] = $userinfo->id;
                $update['user_name'] = $userinfo->name;
                $update['police_id'] = $re->minjing;
                $update['ip'] = get_client_ip();
                $update['updated_time'] = date('Y-m-d H:i:s',time());
                $update_id = db::table('units_update')->insertGetId($update);
                //更改企业状态
                $status['status'] = '5';
                db::table('units')->where('id',$id)->update($status);
            }
            //记录基本信息表修改的字段以及修改前和修改后的值
            foreach ($data as $k=>$v){
                if($re->$k != $v){
                    $update2['pid'] = $update_id;
                    $update2['field'] = $k;
                    $update2['field_name'] = getValue($k,'units_field' ,'field' , 'field_name');
                    $update2['before_value'] = $re->$k;
                    $update2['after_value'] = $v;
                    db::table('units_update_log')->insert($update2);
                }
            }
            //记录行业信息表修改的字段以及修改前和修改后的值
            if($field_data){
                foreach ($field_data as $k=>$v){
                    $array['unit_id'] = $where['unit_id'] = $id;
                    $array['type_id'] = $where['type_id'] = $re->type;
                    $array['field_id'] = $where['field_id'] = db::table('unit_type_field')->where('type_id',$re->type)->where('field_name',$k)->value('id');
                    $result = db::table('unit_type_field_value')->where($where)->first();
                    if($result){
                        if($result->val != $v){
                            $update3['pid'] = $update_id;
                            $update3['field'] = $k;
                            $map3['field_name'] = $k;
                            $map3['type_id'] = $re->type;
                            $update3['field_name'] = db::table('unit_type_field')->where($map3)->value('field_content');
                            $update3['before_value'] = $result->val;
                            $update3['after_value'] = $v;
                            db::table('units_update_log')->insert($update3);
                        }
                        $array['field_name'] = $k;
                        $array['val'] = $v;
                        foreach ($array as $k=>$v){
                            $array[$k] = htmlspecialchars($v);
                        }
                        db::table('unit_type_field_value')->where($where)->update($array);
                    }else{
                        $array['field_name'] = $k;
                        $array['val'] = $v;
                        foreach ($array as $k=>$v){
                            $array[$k] = htmlspecialchars($v);
                        }
                        db::table('unit_type_field_value')->insert($array);
                    }
                }
            }
            return ['status'=>1,'msg'=>'编辑成功'];
        }
        //单位类型
        $type = db::table('unit_type')->get();
        $city = db::table('area')->where('dengji','=','1')->orderby('id')->get();//市
        //要害部位数量
        $count['buwei_num'] = db::table('zhongyaobuwei')->where('unit_id',$id)->count();
        //重点岗位人数
        $count['zdgw_num'] = db::table('zhongdianrenyuan')->where('unit_id',$id)->count();
        //在职保卫人数
        $count['zaizhi_num'] = db::table('baoweirenyuan')->where('unit_id',$id)->where('guanxi','在职职工')->count();
        //自招保安人数
        $count['zizhao_num'] = db::table('baoweirenyuan')->where('unit_id',$id)->where('guanxi','自招')->count();
        //外聘保安人数
        $count['waipin_num'] = db::table('baoweirenyuan')->where('unit_id',$id)->where('guanxi','外聘')->count();
        //民警
        $police = db::table('admin_users')->where('org_id',$re->org_id)->where('role_id',15)->where('dengji','4')->get();
        //行业字段
        $field_count = db::table('unit_type_field')->where('type_id',$re->type)->where('is_show','1')->count();
        if($field_count > 0){
            for ($x=0; $x*2<$field_count; $x++) {
                $field[$x] = db::table('unit_type_field')->where('type_id',$re->type)->where('is_show','1')->orderby('paixu','asc')->offset(2*$x)->limit(2)->get();
            }
            foreach ($field as $k=>$v){
                foreach ($v as $k2=>$v2){
                    if($v2->field_type == 2){
                        $v2->daan = db::table('unit_type_field_daan')->where('pid',$v2->id)->get();
                    }
                }
            }
        }else{
            $field = array();
        }

        return view('admin.units.edit', ['re' => $re,'type' => $type,'city' => $city,'id' => $id,'count'=>$count,'police'=>$police,'field'=>$field]);
    }

    public function history($id,request $request)
    {
        if($request->rows) {
            $map['unit_id'] = $id;
            $res = DB::table('units_history')->where($map)->paginate($request->rows);
            $list = $res->items();
            $result['total'] = $res->total();
            $result['rows'] = $list;
            echo json_encode($result);
        }else{
            return view('admin.units.history', ['id'=>$id]);
        }
    }

    public function view_history($id)
    {
        $re = DB::table('units_history')->where('id',$id)->first();
        //单位类型
        $type = db::table('unit_type')->get();
        $city = db::table('area')->where('dengji','=','1')->orderby('id')->get();//市
        return view('admin.units.view_history', ['re' => $re,'type' => $type,'city' => $city]);
    }

    public function del_history($id)
    {
        $re = db::table('units_history')->where('id',$id)->delete();
        if($re){
            return ['status'=> 1,'msg'=>'删除成功'];
        }else{
            return ['status'=> 0,'msg'=>'删除失败'];
        }
        //return redirect()->back();
    }

    public function zhuxiao($id)
    {
        $data['is_del'] = '1';
        $re = db::table('units')->where('id',$id)->update($data);
        if($re){
            return ['status'=>1,'msg'=>'注销成功'];
        }else{
            return ['status'=>0,'msg'=>'注销失败'];
        }
        //return redirect()->back();
    }

    public function huifu($id)
    {
        $data['is_del'] = '0';
        $re = db::table('units')->where('id',$id)->update($data);
        if($re){
            return ['status'=> 1,'msg'=>'恢复成功'];
        }else{
            return ['status'=> 0,'msg'=>'恢复失败'];
        }
        //return redirect()->back();
    }

    public function del($id)
    {
        $re = db::table('units')->where('id',$id)->delete();
        if($re){
            return ['status'=> 1,'msg'=>'删除成功'];
        }else{
            return ['status'=> 0,'msg'=>'删除失败'];
        }
        //return redirect()->back();
    }

    //设为重点
    public function swzd($id,$jibie)
    {
        $where['id'] = $id;
        $data['is_imp'] = $jibie;
        $re = db::table('units')->where($where)->update($data);
        if($re){
            return ['status'=>1,'msg'=>'操作成功'];
        }else{
            return ['status'=>0,'msg'=>'操作成功'];
        }
        //return redirect()->back();
    }

    //取消重点
    public function qxzd($id)
    {
        $where['id'] = $id;
        $data['is_imp'] = '2';
        db::table('units')->where($where)->update($data);
        return redirect()->back();
    }

    public function faren($id,request $request)
    {
        if($request->rows) {
            $map['unit_id'] = $id;
            $res = DB::table('faren')->where($map)->orderby('id','desc')->paginate($request->rows);
            $list = $res->items();
            $result['total'] = $res->total();
            $result['rows'] = $list;
            echo json_encode($result);
        }else{
            return view('admin.units.faren', ['id'=>$id]);
        }
    }

    public function add_faren(Request $request,$id)
    {
        if($request->isMethod('post')){
            $this->validate($request, [
                'unit_name' => 'required',
                'name' => 'required',
                'sex' => 'required',
                'birthday' => 'required',
                'nation' => 'required',
                'native_place' => 'required',
                'education' => 'required',
                'political_status' => 'required',
                'id_number' =>array('regex:/^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$/'),
                'email' =>'nullable|email',
                'phone' =>array('regex:/^((0\d{2,3}-\d{7,8})|(1[3584]\d{9}))$/'),
            ],$this->messages('faren'));
            $data = Input::except('_token');
            if($request['pic']){
                $images = $request['pic'];
                $filedir = "upload/qiye/".date('Y-m-d',time())."/";
                $extension = $images -> getClientOriginalExtension();
                $newImagesName=md5(time()).random_int(5,5).".".$extension;
                $images->move($filedir,$newImagesName);
                $data['pic'] = $filedir.$newImagesName;
            }
            $data['unit_id'] = $id;
            $data['user_id'] = Auth::guard('admin')->user()->id;
            $data['created_at'] = date('Y-m-d H:i:s',time());
            $data['updated_at'] = date('Y-m-d H:i:s',time());
            foreach ($data as $k=>$v){
                $data[$k] = htmlspecialchars($v);
            }
            $a = DB::table('faren')->insert($data);
            if($a){
                //添加一条日志
                $data2['unit_id'] = $id;
                $data2['status'] = '1';
                $data2['type'] = '1';
                $data2['name'] = Input::get('name');
                $data2['user_id'] = auth::user()->id;
                $data2['ip'] = get_client_ip();
                $data2['updated_time'] = date('Y-m-d H:i:s',time());
                DB::table('units_other_log')->insert($data2);
                return ['msg'=>'添加成功'];
            }else{
                return ['msg'=>'添加失败'];
            }
            //return redirect('admin/units/faren/'.$id);
        }
        //民族
        $nation = db::table('nation')->get();
        return view('admin.units.add_faren', ['nation' => $nation,'id' => $id]);
    }

    public function edit_faren($id,Request $request)
    {
        $re = DB::table('faren')->where('id',$id)->first();
        if($request->isMethod('post')){
            $this->validate($request, [
                'unit_name' => 'required',
                'name' => 'required',
                'sex' => 'required',
                'birthday' => 'required',
                'nation' => 'required',
                'native_place' => 'required',
                'education' => 'required',
                'political_status' => 'required',
                'id_number' =>array('regex:/^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$/'),
                'email' =>'nullable|email',
                'phone' =>array('regex:/^((0\d{2,3}-\d{7,8})|(1[3584]\d{9}))$/'),
            ],$this->messages('faren'));
            $data = Input::except('_token');
            if($request['pic']){
                $images = $request['pic'];
                $filedir = "upload/qiye/".date('Y-m-d',time())."/";
                $extension = $images -> getClientOriginalExtension();
                $newImagesName=md5(time()).random_int(5,5).".".$extension;
                $images->move($filedir,$newImagesName);
                $data['pic'] = $filedir.$newImagesName;
            }
            $data['user_id'] = Auth::guard('admin')->user()->id;
            $data['updated_at'] = date('Y-m-d H:i:s',time());
            foreach ($data as $k=>$v){
                $data[$k] = htmlspecialchars($v);
            }
            $a = DB::table('faren')->where('id',$id)->update($data);
            if($a){
                //添加一条日志
                $data2['unit_id'] = $re->unit_id;
                $data2['status'] = '2';
                $data2['type'] = '1';
                $data2['name'] = $re->name;
                $data2['user_id'] = auth::user()->id;
                $data2['ip'] = get_client_ip();
                $data2['updated_time'] = date('Y-m-d H:i:s',time());
                DB::table('units_other_log')->insert($data2);
                return ['msg'=>'编辑成功'];
            }else{
                return ['msg'=>'编辑失败'];
            }
        }
        //民族
        $nation = db::table('nation')->get();
        return view('admin.units.edit_faren', ['re' => $re,'nation' => $nation,'id' => $id]);
    }

    public function del_faren($id)
    {
        $res = DB::table('faren')->where('id',$id)->first();
        $re = db::table('faren')->where('id',$id)->delete();
        if($re){
            //添加一条日志
            $data2['unit_id'] = $res->unit_id;
            $data2['status'] = '3';
            $data2['type'] = '1';
            $data2['name'] = $res->name;
            $data2['user_id'] = auth::user()->id;
            $data2['ip'] = get_client_ip();
            $data2['updated_time'] = date('Y-m-d H:i:s',time());
            DB::table('units_other_log')->insert($data2);
            return ['status'=> 1,'msg'=>'删除成功'];
        }else{
            return ['status'=> 0,'msg'=>'删除失败'];
        }
        //return redirect()->back();
    }

    public function baowei($id,request $request)
    {
        if($request->rows) {
            $map['unit_id'] = $id;
            $res = DB::table('baoweirenyuan')->where($map)->paginate($request->rows);
            $list = $res->items();
            $result['total'] = $res->total();
            $result['rows'] = $list;
            echo json_encode($result);
        }else{
            return view('admin.units.baowei', ['id'=>$id]);
        }
    }

    public function add_baowei(Request $request,$id)
    {
        if($request->isMethod('post')){
            $this->validate($request, [
                'unit_name' => 'required',
                'name' => 'required',
                'sex' => 'required',
                'birthday' => 'required',
                'nation' => 'required',
                'native_place' => 'required',
                'education' => 'required',
                'political_status' => 'required',
                'id_number' =>array('regex:/^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$/'),
                'email' =>'nullable|email',
                'phone' =>array('regex:/^((0\d{2,3}-\d{7,8})|(1[3584]\d{9}))$/'),
            ],$this->messages('faren'));
            $data = Input::except('_token');
            $unit_info = db::table('units')->where('id',$id)->first();
            if($unit_info->org_id){
                $data['city'] = $unit_info->city;
                $data['area'] = $unit_info->area;
                $data['org_id'] = $unit_info->org_id;
                $data['unit_name'] = $unit_info->name;
            }else{
                return ['status'=>0,'msg'=>'请先完善单位市区派出所信息'];
            }
            if($request['pic']){
                $images = $request['pic'];
                $filedir = "upload/qiye/".date('Y-m-d',time())."/";
                $extension = $images -> getClientOriginalExtension();
                $newImagesName=md5(time()).random_int(5,5).".".$extension;
                $images->move($filedir,$newImagesName);
                $data['pic'] = $filedir.$newImagesName;
            }
            $data['unit_id'] = $id;
            $data['user_id'] = Auth::guard('admin')->user()->id;
            $data['created_at'] = date('Y-m-d H:i:s',time());
            $data['updated_at'] = date('Y-m-d H:i:s',time());
            foreach ($data as $k=>$v){
                $data[$k] = htmlspecialchars($v);
            }
            $a = DB::table('baoweirenyuan')->insert($data);
            if($a){
                //添加一条日志
                $data2['unit_id'] = $id;
                $data2['status'] = '1';
                $data2['type'] = '2';
                $data2['name'] = Input::get('name');
                $data2['user_id'] = auth::user()->id;
                $data2['ip'] = get_client_ip();
                $data2['updated_time'] = date('Y-m-d H:i:s',time());
                DB::table('units_other_log')->insert($data2);
                return ['status'=>1,'msg'=>'保存成功'];
            }else{
                return ['status'=>0,'msg'=>'保存失败'];
            }
            //return redirect('admin/units/baowei/'.$id);
        }
        //民族
        $nation = db::table('nation')->get();
        //保安
        $baoan = db::table('baoan')->get();
        return view('admin.units.add_baowei', ['id'=>$id,'nation'=>$nation,'baoan'=>$baoan]);
    }

    public function edit_baowei($id,Request $request)
    {
        $re = DB::table('baoweirenyuan')->where('id',$id)->first();
        if($request->isMethod('post')){
            $this->validate($request, [
                'unit_name' => 'required',
                'name' => 'required',
                'sex' => 'required',
                'birthday' => 'required',
                'nation' => 'required',
                'native_place' => 'required',
                'education' => 'required',
                'political_status' => 'required',
                'id_number' =>array('regex:/^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$/'),
                'email' =>'nullable|email',
                'phone' =>array('regex:/^((0\d{2,3}-\d{7,8})|(1[3584]\d{9}))$/'),
            ],$this->messages('faren'));
            $data = Input::except('_token');
            if($request['pic']){
                $images = $request['pic'];
                $filedir = "upload/qiye/".date('Y-m-d',time())."/";
                $extension = $images -> getClientOriginalExtension();
                $newImagesName=md5(time()).random_int(5,5).".".$extension;
                $images->move($filedir,$newImagesName);
                $data['pic'] = $filedir.$newImagesName;
            }
            $data['user_id'] = Auth::guard('admin')->user()->id;
            $data['updated_at'] = date('Y-m-d H:i:s',time());
            foreach ($data as $k=>$v){
                $data[$k] = htmlspecialchars($v);
            }
            $a = DB::table('baoweirenyuan')->where('id',$id)->update($data);
            if($a){
                //添加一条日志
                $data2['unit_id'] = $re->unit_id;
                $data2['status'] = '2';
                $data2['type'] = '2';
                $data2['name'] = $re->name;
                $data2['user_id'] = auth::user()->id;
                $data2['ip'] = get_client_ip();
                $data2['updated_time'] = date('Y-m-d H:i:s',time());
                DB::table('units_other_log')->insert($data2);
                return ['msg'=>'编辑成功'];
            }else{
                return ['msg'=>'编辑失败'];
            }
            //return redirect('admin/units/baowei/'.$cid);
        }
        $nation = db::table('nation')->get();
        //保安
        $baoan = db::table('baoan')->get();
        return view('admin.units.edit_baowei', ['re' => $re,'id'=>$id,'nation'=>$nation,'baoan'=>$baoan]);
    }

    public function del_baowei($id)
    {
        $res = DB::table('baoweirenyuan')->where('id',$id)->first();
        $re = db::table('baoweirenyuan')->where('id',$id)->delete();
        if($re){
            //添加一条日志
            $data2['unit_id'] = $res->unit_id;
            $data2['status'] = '3';
            $data2['type'] = '2';
            $data2['name'] = $res->name;
            $data2['user_id'] = auth::user()->id;
            $data2['ip'] = get_client_ip();
            $data2['updated_time'] = date('Y-m-d H:i:s',time());
            DB::table('units_other_log')->insert($data2);
            return ['status'=> 1,'msg'=>'删除成功'];
        }else{
            return ['status'=> 0,'msg'=>'删除失败'];
        }
        //return redirect()->back();
    }

    public function jigou($id,request $request)
    {
        if($request->rows) {
            $map['unit_id'] = $id;
            $res = DB::table('jigou')->where($map)->paginate($request->rows);
            $list = $res->items();
            $result['total'] = $res->total();
            $result['rows'] = $list;
            echo json_encode($result);
        }else{
            return view('admin.units.jigou', ['id'=>$id]);
        }
    }

    public function add_jigou(Request $request,$id)
    {
        if($request->isMethod('post')){
            $data = Input::except('_token');
            $data['unit_id'] = $id;
            $data['user_id'] = Auth::guard('admin')->user()->id;
            $data['created_at'] = date('Y-m-d H:i:s',time());
            $data['updated_at'] = date('Y-m-d H:i:s',time());

            $re = DB::table('jigou')->insert($data);
            if($re){
                return ['msg'=>'添加成功'];
            }else{
                return ['msg'=>'添加失败'];
            }
            //return redirect('admin/units/jigou/'.$id);
        }
        return view('admin.units.add_jigou',['id'=>$id]);
    }

    public function edit_jigou($id,Request $request)
    {
        if($request->isMethod('post')){
            $data = Input::except('_token');
            $data['user_id'] = Auth::guard('admin')->user()->id;
            $data['updated_at'] = date('Y-m-d H:i:s',time());
            $re = DB::table('jigou')->where('id',$id)->update($data);
            if($re){
                return ['msg'=>'编辑成功'];
            }else{
                return ['msg'=>'编辑失败'];
            }
            //$cid = DB::table('jigou')->where('id',$id)->value('unit_id');
            //return redirect('admin/units/jigou/'.$cid);
        }
        $re = DB::table('jigou')->where('id',$id)->first();
        return view('admin.units.edit_jigou', ['re' => $re,'id'=>$id]);
    }

    public function del_jigou($id)
    {
        $re = db::table('jigou')->where('id',$id)->delete();
        if($re){
            return ['status'=> 1,'msg'=>'删除成功'];
        }else{
            return ['status'=> 0,'msg'=>'删除失败'];
        }
        //return redirect()->back();
    }

    public function zybw($id,request $request)
    {
        if($request->rows) {
            $map['unit_id'] = $id;
            $res = DB::table('zhongyaobuwei')->where($map)->paginate($request->rows);
            $list = $res->items();
            $result['total'] = $res->total();
            $result['rows'] = $list;
            echo json_encode($result);
        }else{
            return view('admin.units.zybw', ['id'=>$id]);
        }
    }

    public function add_zybw(Request $request,$id)
    {
        if($request->isMethod('post')){
            $this->validate($request, [
                'unit_name' => 'required',
                'name' => 'required',
                'fuzeren' => 'required',
                'dengji' => 'required',
                'type' => 'required',
                'aqff_01' => 'required',
                'aqff_02' => 'required',
                'aqff_03' => 'required',
                'aqff_04' => 'required',
                'aqff_05' => 'required',
                'aqff_06' => 'required',
                'aqff_07' => 'required',
                'aqff_08' => 'required',
                'aqff_09' => 'required',
                'aqff_10' => 'required',
                'aqff_11' => 'required',
                'aqff_12' => 'required',
                'aqff_13' => 'required',
                'aqff_14' => 'required',
            ],$this->messages('buwei'));
            /*$re = db::table('xiansuo')->where('police_cl','1')->get();
            foreach ($re as $k=>$v){
                $data['chuli_time'] = strtotime($v->update_at) - strtotime($v->created_at);
                //$data['city'] = getValue($v->unit_id,'units','id','city');
                //$data['area'] = getValue($v->unit_id,'units','id','area');
                //$data['org_id'] = getValue($v->unit_id,'units','id','org_id');
                //$data['unit_name'] = getValue($v->unit_id,'units','id','name');
                db::table('xiansuo')->where('xs_id',$v->xs_id)->update($data);
            }
            dd('1');*/
            $data = Input::except('_token');
            $unit_info = db::table('units')->where('id',$id)->first();
            if($unit_info->org_id){
                $data['city'] = $unit_info->city;
                $data['area'] = $unit_info->area;
                $data['org_id'] = $unit_info->org_id;
                $data['unit_name'] = $unit_info->name;
            }else{
                return ['status'=>0,'msg'=>'请先完善单位市区派出所信息'];
            }
            if($request['pic1']){
                $images = $request['pic1'];
                $filedir = "upload/qiye/".date('Y-m-d',time())."/";
                $extension = $images -> getClientOriginalExtension();
                $newImagesName=md5(time()).random_int(5,5).".".$extension;
                $images->move($filedir,$newImagesName);
                $data['pic1'] = $filedir.$newImagesName;
            }
            /*if($request['pic2']){
                $images = $request['pic2'];
                $filedir = "upload/qiye/".date('Y-m-d',time())."/";
                $extension = $images -> getClientOriginalExtension();
                $newImagesName=md5(time()).random_int(5,5).".".$extension;
                $images->move($filedir,$newImagesName);
                $data['pic2'] = $filedir.$newImagesName;
            }
            if($request['pic3']){
                $images = $request['pic3'];
                $filedir = "upload/qiye/".date('Y-m-d',time())."/";
                $extension = $images -> getClientOriginalExtension();
                $newImagesName=md5(time()).random_int(5,5).".".$extension;
                $images->move($filedir,$newImagesName);
                $data['pic3'] = $filedir.$newImagesName;
            }*/
            $data['unit_id'] = $id;
            $data['user_id'] = Auth::guard('admin')->user()->id;
            $data['created_at'] = date('Y-m-d H:i:s',time());
            $data['updated_at'] = date('Y-m-d H:i:s',time());
            foreach ($data as $k=>$v){
                $data[$k] = htmlspecialchars($v);
            }
            $a = DB::table('zhongyaobuwei')->insert($data);
            if($a){
                //添加一条日志
                $data2['unit_id'] = $id;
                $data2['status'] = '1';
                $data2['type'] = '4';
                $data2['name'] = Input::get('name');
                $data2['user_id'] = auth::user()->id;
                $data2['ip'] = get_client_ip();
                $data2['updated_time'] = date('Y-m-d H:i:s',time());
                DB::table('units_other_log')->insert($data2);
                return ['status'=>1,'msg'=>'添加成功'];
            }else{
                return ['status'=>0,'msg'=>'添加失败'];
            }
            //return redirect('admin/units/zybw/'.$id);
        }
        //类型
        $type = db::table('buwei_type')->get();
        return view('admin.units.add_zybw', ['id'=>$id,'type'=>$type]);
    }

    public function edit_zybw($id,Request $request)
    {
        $re = DB::table('zhongyaobuwei')->where('id',$id)->first();
        if($request->isMethod('post')){
            $this->validate($request, [
                'unit_name' => 'required',
                'name' => 'required',
                'fuzeren' => 'required',
                'dengji' => 'required',
                'type' => 'required',
                'aqff_01' => 'required',
                'aqff_02' => 'required',
                'aqff_03' => 'required',
                'aqff_04' => 'required',
                'aqff_05' => 'required',
                'aqff_06' => 'required',
                'aqff_07' => 'required',
                'aqff_08' => 'required',
                'aqff_09' => 'required',
                'aqff_10' => 'required',
                'aqff_11' => 'required',
                'aqff_12' => 'required',
                'aqff_13' => 'required',
                'aqff_14' => 'required',
            ],$this->messages('buwei'));
            $data = Input::except('_token');
            if($request['pic1']){
                $images = $request['pic1'];
                $filedir = "upload/qiye/".date('Y-m-d',time())."/";
                $extension = $images -> getClientOriginalExtension();
                $newImagesName=md5(time()).random_int(5,5).".".$extension;
                $images->move($filedir,$newImagesName);
                $data['pic1'] = $filedir.$newImagesName;
            }
            /*if($request['pic2']){
                $images = $request['pic2'];
                $filedir = "upload/qiye/".date('Y-m-d',time())."/";
                $extension = $images -> getClientOriginalExtension();
                $newImagesName=md5(time()).random_int(5,5).".".$extension;
                $images->move($filedir,$newImagesName);
                $data['pic2'] = $filedir.$newImagesName;
            }
            if($request['pic3']){
                $images = $request['pic3'];
                $filedir = "upload/qiye/".date('Y-m-d',time())."/";
                $extension = $images -> getClientOriginalExtension();
                $newImagesName=md5(time()).random_int(5,5).".".$extension;
                $images->move($filedir,$newImagesName);
                $data['pic3'] = $filedir.$newImagesName;
            }*/
            /*if($request['yuan']){
                $images = $request['yuan'];
                $filedir = "upload/qiye/".date('Y-m-d',time())."/";
                $extension = $images -> getClientOriginalExtension();
                $newImagesName=md5(time()).random_int(5,5).".".$extension;
                $images->move($filedir,$newImagesName);
                $data['yuan'] = $filedir.$newImagesName;
            }*/
            $data['user_id'] = Auth::guard('admin')->user()->id;
            $data['updated_at'] = date('Y-m-d H:i:s',time());
            foreach ($data as $k=>$v){
                $data[$k] = htmlspecialchars($v);
            }
            $a = DB::table('zhongyaobuwei')->where('id',$id)->update($data);
            if($a){
                //添加一条日志
                $data2['unit_id'] = $re->unit_id;
                $data2['status'] = '2';
                $data2['type'] = '4';
                $data2['name'] = $re->name;
                $data2['user_id'] = auth::user()->id;
                $data2['ip'] = get_client_ip();
                $data2['updated_time'] = date('Y-m-d H:i:s',time());
                DB::table('units_other_log')->insert($data2);
                return ['msg'=>'编辑成功'];
            }else{
                return ['msg'=>'编辑失败'];
            }
            //$cid = DB::table('zhongyaobuwei')->where('id',$id)->value('unit_id');
            //return redirect('admin/units/zybw/'.$cid);
        }
        //类型
        $type = db::table('buwei_type')->get();
        return view('admin.units.edit_zybw', ['re' => $re,'id'=>$id,'type'=>$type]);
    }

    public function del_zybw($id)
    {
        $res = DB::table('zhongyaobuwei')->where('id',$id)->first();
        $re = db::table('zhongyaobuwei')->where('id',$id)->delete();
        if($re){
            //添加一条日志
            $data2['unit_id'] = $res->unit_id;
            $data2['status'] = '3';
            $data2['type'] = '4';
            $data2['name'] = $res->name;
            $data2['user_id'] = auth::user()->id;
            $data2['ip'] = get_client_ip();
            $data2['updated_time'] = date('Y-m-d H:i:s',time());
            DB::table('units_other_log')->insert($data2);
            return ['status'=> 1,'msg'=>'删除成功'];
        }else{
            return ['status'=> 0,'msg'=>'删除失败'];
        }
        //return redirect()->back();
    }

    public function gzry($id,request $request)
    {
        if($request->rows) {
            $map['buwei_id'] = $id;
            $res = DB::table('gongzuorenyuan')->where($map)->paginate($request->rows);
            $list = $res->items();
            $result['total'] = $res->total();
            $result['rows'] = $list;
            echo json_encode($result);
        }else{
            return view('admin.units.gzry', ['id'=>$id]);
        }
    }

    public function add_gzry(Request $request,$id)
    {
        $unit_id = DB::table('zhongyaobuwei')->where('id',$id)->value('unit_id');
        if($request->isMethod('post')){
            $data = Input::except('_token');
            if($request['pic']){
                $images = $request['pic'];
                $filedir = "upload/qiye/".date('Y-m-d',time())."/";
                $extension = $images -> getClientOriginalExtension();
                $newImagesName=md5(time()).random_int(5,5).".".$extension;
                $images->move($filedir,$newImagesName);
                $data['pic'] = $filedir.$newImagesName;
            }
            $data['buwei_id'] = $id;
            $data['unit_id'] = $unit_id;
            $data['user_id'] = Auth::guard('admin')->user()->id;
            $data['created_at'] = date('Y-m-d H:i:s',time());
            $data['updated_at'] = date('Y-m-d H:i:s',time());

            $re = DB::table('gongzuorenyuan')->insert($data);
            if($re){
                return ['msg'=>'添加成功'];
            }else{
                return ['msg'=>'添加失败'];
            }
            //return redirect('admin/units/gzry/'.$id);
        }
        //民族
        $nation = db::table('nation')->get();
        return view('admin.units.add_gzry',['nation' => $nation,'id'=>$id]);
    }

    public function edit_gzry($id,Request $request)
    {
        if($request->isMethod('post')){
            $data = Input::except('_token');
            if($request['pic']){
                $images = $request['pic'];
                $filedir = "upload/qiye/".date('Y-m-d',time())."/";
                $extension = $images -> getClientOriginalExtension();
                $newImagesName=md5(time()).random_int(5,5).".".$extension;
                $images->move($filedir,$newImagesName);
                $data['pic'] = $filedir.$newImagesName;
            }
            $data['user_id'] = Auth::guard('admin')->user()->id;
            $data['updated_at'] = date('Y-m-d H:i:s',time());
            $re = DB::table('gongzuorenyuan')->where('id',$id)->update($data);
            if($re){
                return ['msg'=>'编辑成功'];
            }else{
                return ['msg'=>'编辑失败'];
            }
            //$cid = DB::table('gongzuorenyuan')->where('id',$id)->value('buwei_id');
            //return redirect('admin/units/gzry/'.$cid);
        }
        $re = DB::table('gongzuorenyuan')->where('id',$id)->first();
        //民族
        $nation = db::table('nation')->get();
        return view('admin.units.edit_gzry', ['re' => $re,'id'=>$id,'nation'=>$nation]);
    }

    public function del_gzry($id)
    {
        $re = db::table('gongzuorenyuan')->where('id',$id)->delete();
        if($re){
            return ['status'=> 1,'msg'=>'删除成功'];
        }else{
            return ['status'=> 0,'msg'=>'删除失败'];
        }
        //return redirect()->back();
    }

    public function edit_anfang($id,Request $request)
    {
        $type = db::table('units')->where('id',$id)->value('type');
        if(!$type){
            echo "<script>alert('确认单位类型后才能编辑安防设施情况');window.history.go(-1);</script>";
            exit();
        }
        if($request->isMethod('post')){
            $data = Input::except('_token');
            $data['user_id'] = Auth::guard('admin')->user()->id;
            $data['updated_at'] = date('Y-m-d H:i:s',time());
            $data['unit_id'] = $map['unit_id'] = $id;
            //查询表中是否有数据，如没有则增加一条
            $msg = db::table('anfangsheshi')->where($map)->first();
            if(!$msg){
                $re = DB::table('anfangsheshi')->insert($data);
            }else{
                $re = DB::table('anfangsheshi')->where('unit_id',$id)->update($data);
            }
            if($re){
                return ['msg'=>'编辑成功'];
            }else{
                return ['msg'=>'编辑失败'];
            }
            //return redirect('admin/units/edit_anfang/'.$id);
        }
        $re = DB::table('anfangsheshi')->where('unit_id',$id)->first();
        //dd($re);
        return view('admin.units.edit_anfang', ['re' => $re,'type' => $type,'id'=>$id]);
    }

    public function zdry($id,request $request)
    {
        if($request->rows) {
            $map['unit_id'] = $id;
            $res = DB::table('zhongdianrenyuan')->where($map)->paginate($request->rows);
            $list = $res->items();
            $result['total'] = $res->total();
            $result['rows'] = $list;
            echo json_encode($result);
        }else{
            return view('admin.units.zdry', ['id'=>$id]);
        }
    }

    public function add_zdry(Request $request,$id)
    {
        if($request->isMethod('post')){
            $this->validate($request, [
                'unit_name' => 'required',
                'name' => 'required',
                'sex' => 'required',
                'birthday' => 'required',
                'nation' => 'required',
                'native_place' => 'required',
                'education' => 'required',
                'political_status' => 'required',
                'id_number' =>array('regex:/^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$/'),
                'email' =>'nullable|email',
                'phone' =>array('regex:/^((0\d{2,3}-\d{7,8})|(1[3584]\d{9}))$/'),
            ],$this->messages('faren'));
            $data = Input::except('_token');
            $unit_info = db::table('units')->where('id',$id)->first();
            if($unit_info->org_id){
                $data['city'] = $unit_info->city;
                $data['area'] = $unit_info->area;
                $data['org_id'] = $unit_info->org_id;
                $data['unit_name'] = $unit_info->name;
            }else{
                return ['status'=>0,'msg'=>'请先完善单位市区派出所信息'];
            }
            if($request['pic']){
                $images = $request['pic'];
                $filedir = "upload/qiye/".date('Y-m-d',time())."/";
                $extension = $images -> getClientOriginalExtension();
                $newImagesName=md5(time()).random_int(5,5).".".$extension;
                $images->move($filedir,$newImagesName);
                $data['pic'] = $filedir.$newImagesName;
            }
            $data['unit_id'] = $id;
            $data['user_id'] = Auth::guard('admin')->user()->id;
            $data['created_at'] = date('Y-m-d H:i:s',time());
            $data['updated_at'] = date('Y-m-d H:i:s',time());
            foreach ($data as $k=>$v){
                $data[$k] = htmlspecialchars($v);
            }
            $a = DB::table('zhongdianrenyuan')->insert($data);
            if($a){
                //添加一条日志
                $data2['unit_id'] = $id;
                $data2['status'] = '1';
                $data2['type'] = '3';
                $data2['name'] = Input::get('name');
                $data2['user_id'] = auth::user()->id;
                $data2['ip'] = get_client_ip();
                $data2['updated_time'] = date('Y-m-d H:i:s',time());
                DB::table('units_other_log')->insert($data2);
                return ['status'=>1,'msg'=>'添加成功'];
            }else{
                return ['status'=>0,'msg'=>'添加失败'];
            }
            //return redirect('admin/units/zdry/'.$id);
        }
        //民族
        $nation = db::table('nation')->get();
        return view('admin.units.add_zdry',['id' => $id,'nation'=>$nation]);
    }

    public function edit_zdry($id,Request $request)
    {
        $re = DB::table('zhongdianrenyuan')->where('id',$id)->first();
        if($request->isMethod('post')){
            $this->validate($request, [
                'unit_name' => 'required',
                'name' => 'required',
                'sex' => 'required',
                'birthday' => 'required',
                'nation' => 'required',
                'native_place' => 'required',
                'education' => 'required',
                'political_status' => 'required',
                'id_number' =>array('regex:/^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$/'),
                'email' =>'nullable|email',
                'phone' =>array('regex:/^((0\d{2,3}-\d{7,8})|(1[3584]\d{9}))$/'),
            ],$this->messages('faren'));
            $data = Input::except('_token');
            if($request['pic']){
                $images = $request['pic'];
                $filedir = "upload/qiye/".date('Y-m-d',time())."/";
                $extension = $images -> getClientOriginalExtension();
                $newImagesName=md5(time()).random_int(5,5).".".$extension;
                $images->move($filedir,$newImagesName);
                $data['pic'] = $filedir.$newImagesName;
            }
            $data['user_id'] = Auth::guard('admin')->user()->id;
            $data['updated_at'] = date('Y-m-d H:i:s',time());
            foreach ($data as $k=>$v){
                $data[$k] = htmlspecialchars($v);
            }
            $a = DB::table('zhongdianrenyuan')->where('id',$id)->update($data);
            if($a){
                //添加一条日志
                $data2['unit_id'] = $re->unit_id;
                $data2['status'] = '2';
                $data2['type'] = '3';
                $data2['name'] = $re->name;
                $data2['user_id'] = auth::user()->id;
                $data2['ip'] = get_client_ip();
                $data2['updated_time'] = date('Y-m-d H:i:s',time());
                DB::table('units_other_log')->insert($data2);
                return ['status'=>1,'msg'=>'编辑成功'];
            }else{
                return ['status'=>0,'msg'=>'编辑失败'];
            }
        }
        //民族
        $nation = db::table('nation')->get();
        return view('admin.units.edit_zdry', ['re' => $re,'id' => $id,'nation'=>$nation]);
    }

    public function del_zdry($id)
    {
        $res = DB::table('zhongdianrenyuan')->where('id',$id)->first();
        $re = db::table('zhongdianrenyuan')->where('id',$id)->delete();
        if($re){
            //添加一条日志
            $data2['unit_id'] = $res->unit_id;
            $data2['status'] = '3';
            $data2['type'] = '3';
            $data2['name'] = $res->name;
            $data2['user_id'] = auth::user()->id;
            $data2['ip'] = get_client_ip();
            $data2['updated_time'] = date('Y-m-d H:i:s',time());
            DB::table('units_other_log')->insert($data2);
            return ['status'=> 1,'msg'=>'删除成功'];
        }else{
            return ['status'=> 0,'msg'=>'删除失败'];
        }
        //return redirect()->back();
    }

    public function tfsj($id,request $request)
    {
        if($request->rows) {
            $map['unit_id'] = $id;
            $res = DB::table('tufashijianyuan')->where($map)->paginate($request->rows);
            $list = $res->items();
            $result['total'] = $res->total();
            $result['rows'] = $list;
            echo json_encode($result);
        }else{
            return view('admin.units.tfsj', ['id'=>$id]);
        }
    }

    public function add_tfsj(Request $request,$id)
    {
        if($request->isMethod('post')){
            $this->validate($request, [
                'unit_name' => 'required',
                'name' => 'required',
                'bianzhiren' => 'required',
                'bianzhi_time' => 'required',
                'content' => 'required',
            ],$this->messages('yuan'));
            $data = Input::except('_token');
            if($request['yuan']){
                $images = $request['yuan'];
                $filedir = "upload/qiye/".date('Y-m-d',time())."/";
                $extension = $images -> getClientOriginalExtension();
                $newImagesName=md5(time()).random_int(5,5).".".$extension;
                $images->move($filedir,$newImagesName);
                $data['yuan'] = $filedir.$newImagesName;
            }
            $data['unit_id'] = $id;
            $data['user_id'] = Auth::guard('admin')->user()->id;
            $data['created_at'] = date('Y-m-d H:i:s',time());
            $data['updated_at'] = date('Y-m-d H:i:s',time());
            foreach ($data as $k=>$v){
                $data[$k] = htmlspecialchars($v);
            }
            $re = DB::table('tufashijianyuan')->insert($data);
            if($re){
                return ['msg'=>'添加成功'];
            }else{
                return ['msg'=>'添加失败'];
            }
            //return redirect('admin/units/tfsj/'.$id);
        }
        return view('admin.units.add_tfsj', ['id'=>$id]);
    }

    public function edit_tfsj($id,Request $request)
    {
        if($request->isMethod('post')){
            $this->validate($request, [
                'unit_name' => 'required',
                'name' => 'required',
                'bianzhiren' => 'required',
                'bianzhi_time' => 'required',
                'content' => 'required',
            ],$this->messages('yuan'));
            $data = Input::except('_token');
            if($request['yuan']){
                $images = $request['yuan'];
                $filedir = "upload/qiye/".date('Y-m-d',time())."/";
                $extension = $images -> getClientOriginalExtension();
                $newImagesName=md5(time()).random_int(5,5).".".$extension;
                $images->move($filedir,$newImagesName);
                $data['yuan'] = $filedir.$newImagesName;
            }
            $data['user_id'] = Auth::guard('admin')->user()->id;
            $data['updated_at'] = date('Y-m-d H:i:s',time());
            foreach ($data as $k=>$v){
                $data[$k] = htmlspecialchars($v);
            }
            $re = DB::table('tufashijianyuan')->where('id',$id)->update($data);
            if($re){
                return ['msg'=>'编辑成功'];
            }else{
                return ['msg'=>'编辑失败'];
            }
            //$cid = DB::table('tufashijianyuan')->where('id',$id)->value('unit_id');
            //return redirect('admin/units/tfsj/'.$cid);
        }
        $re = DB::table('tufashijianyuan')->where('id',$id)->first();
        return view('admin.units.edit_tfsj', ['re' => $re,'id'=>$id]);
    }

    public function del_tfsj($id)
    {
        $re = db::table('tufashijianyuan')->where('id',$id)->delete();
        if($re){
            return ['status'=> 1,'msg'=>'删除成功'];
        }else{
            return ['status'=> 0,'msg'=>'删除失败'];
        }
        //return redirect()->back();
    }

    public function zdqk($id,request $request)
    {
        if($request->rows) {
            $map['unit_id'] = $id;
            $res = DB::table('zhongdaqingkuang')->where($map)->paginate($request->rows);
            $list = $res->items();
            $result['total'] = $res->total();
            $result['rows'] = $list;
            echo json_encode($result);
        }else{
            return view('admin.units.zdqk', ['id'=>$id]);
        }
    }

    public function add_zdqk(Request $request,$id)
    {
        if($request->isMethod('post')){
            $data = Input::except('_token');
            $data['unit_id'] = $id;
            $data['user_id'] = Auth::guard('admin')->user()->id;
            $data['created_at'] = date('Y-m-d H:i:s',time());
            $data['updated_at'] = date('Y-m-d H:i:s',time());

            $re = DB::table('zhongdaqingkuang')->insert($data);
            if($re){
                return ['msg'=>'添加成功'];
            }else{
                return ['msg'=>'添加失败'];
            }
            //return redirect('admin/units/zdqk/'.$id);
        }
        return view('admin.units.add_zdqk', ['id'=>$id]);
    }

    public function edit_zdqk($id,Request $request)
    {
        if($request->isMethod('post')){
            $data = Input::except('_token');
            $data['user_id'] = Auth::guard('admin')->user()->id;
            $data['updated_at'] = date('Y-m-d H:i:s',time());
            $re = DB::table('zhongdaqingkuang')->where('id',$id)->update($data);
            if($re){
                return ['msg'=>'编辑成功'];
            }else{
                return ['msg'=>'编辑失败'];
            }
            //$cid = DB::table('zhongdaqingkuang')->where('id',$id)->value('unit_id');
            //return redirect('admin/units/zdqk/'.$cid);
        }
        $re = DB::table('zhongdaqingkuang')->where('id',$id)->first();
        return view('admin.units.edit_zdqk', ['re' => $re,'id'=>$id]);
    }

    public function del_zdqk($id)
    {
        $re = db::table('zhongdaqingkuang')->where('id',$id)->delete();
        if($re){
            return ['status'=> 1,'msg'=>'删除成功'];
        }else{
            return ['status'=> 0,'msg'=>'删除失败'];
        }
        //return redirect()->back();
    }

    //警务责任区认领
    public function renling_units()
    {
        if(Auth::guard('admin')->user()->org_id == null){
            echo "<script>alert('您当前没有组织，暂不能认领单位');window.history.go(-1);</script>";
            exit();
        }else{
            $map['area_id'] = Auth::guard('admin')->user()->org_id;
        }
        $map['org_id'] = null;
        $map['is_del'] = '0';
        $res = DB::table('units')->where($map)->orderby('id','desc')->paginate(10);
        $count = DB::table('units')->where($map)->count();
        return view('admin.units.renling_units', ['res' => $res,'count'=>$count]);
    }

    public function renling($id)
    {
        if(Auth::guard('admin')->user()->org_id == null){
            return ['status'=>0,'msg'=>'您当前没有组织，暂不能认领单位'];
            //echo "<script>alert('您当前没有组织，暂不能认领单位');window.history.go(-1);</script>";
            //exit();
        }
        $unit_orgid = db::table('units')->where('id',$id)->value('org_id');
        if($unit_orgid != Auth::guard('admin')->user()->org_id){
            return ['status'=>0,'msg'=>'只能认领所在派出所辖区内的单位'];
        }
        $where['id'] = $id;
        $data['is_renling'] = '1';
        $re = db::table('units')->where($where)->update($data);
        if($re){
            return ['status'=>1,'msg'=>'认领成功'];
        }else{
            return ['status'=>0,'msg'=>'已认领，请勿重复操作'];
        }
        //return redirect()->back();
    }

    //警务责任区释放
    public function shifang_units()
    {
        if(Auth::guard('admin')->user()->org_id == null){
            echo "<script>alert('您当前没有组织，暂不能释放单位');window.history.go(-1);</script>";
            exit();
        }else{
            $map['area_id'] = Auth::guard('admin')->user()->org_id;
        }
        $map['org_id'] = Auth::guard('admin')->user()->org_id;
        $map['is_del'] = '0';
        $res = DB::table('units')->where($map)->orderby('id','desc')->paginate(10);
        $count = DB::table('units')->where($map)->count();
        return view('admin.units.shifang_units', ['res' => $res,'count'=>$count]);
    }

    public function shifang($id)
    {
        if(Auth::guard('admin')->user()->org_id == null){
            return ['status'=>0,'msg'=>'您当前没有组织，暂不能释放单位'];
        }
        $where['id'] = $id;
        $unit_orgid = db::table('units')->where('id',$id)->value('org_id');
        if($unit_orgid != Auth::guard('admin')->user()->org_id){
            return ['status'=>0,'msg'=>'只能释放所在派出所辖区内的单位'];
        }

        $where['id'] = $id;
        $data['is_renling'] = '0';
        $re = db::table('units')->where($where)->update($data);
        if($re){
            return ['status'=>1,'msg'=>'释放成功'];
        }else{
            return ['status'=>0,'msg'=>'已释放，请勿重复操作'];
        }
        //return redirect()->back();
    }

    //警务责任区变更
    public function biangeng_units()
    {
        if(Auth::guard('admin')->user()->org_id != null){
            $map['org_id'] = Auth::guard('admin')->user()->org_id;
        }
        $map['is_del'] = '0';
        $res = DB::table('units')->where($map)->orderby('id','desc')->paginate(10);
        $count = DB::table('units')->where($map)->count();

        return view('admin.units.biangeng_units', ['res' => $res,'count'=>$count]);
    }

    public function biangeng($id,Request $request)
    {
        $where['id'] = $id;
        $re = db::table('units')->where($where)->value('area_id');
        if($request->isMethod('post')){
            $data = Input::except('_token');
            DB::table('units')->where($where)->update($data);
            return redirect('admin/units/biangeng_units');
        }
        return view('admin.units.biangeng', ['re' => $re]);
    }

    public function closed(request $request)
    {
        if($request->rows) {
            //如果是民警登录，只能查看自己辖区的单位
            if(Auth::guard('admin')->user()->org_id != null && Auth::guard('admin')->user()->dengji == 4){
                $map['units.org_id'] = Auth::guard('admin')->user()->org_id;
            }
            //如果是企业用户
            if(Auth::guard('admin')->user()->role_id == 22){
                $map['units.id'] = Auth::guard('admin')->user()->unit_id;
            }
            if($request->city){
                $map['units.city'] = $request->city;
            }
            if($request->area){
                $map['units.area'] = $request->area;
            }
            if($request->org_id){
                $map['units.org_id'] = $request->org_id;
            }
            if($request->name){
                $map[] = array('units.name','like','%'.$request->name.'%');
            }
            if($request->type){
                $map['units.type'] = $request->type;
            }
            if($request->xingzhi){
                $map['units.xingzhi'] = $request->xingzhi;
            }
            if($request->input('jibie') != null){
                $map['is_imp'] = $request->jibie;
            }
            $map['units.is_del'] = '1';
            $res = DB::table('units')
                ->where($map)
                ->leftJoin('area as a', 'units.city', '=', 'a.id')
                ->leftJoin('area as b', 'units.area', '=', 'b.id')
                ->leftJoin('area as c', 'units.org_id', '=', 'c.id')
                ->leftJoin('unit_type as d', 'units.type', '=', 'd.id')
                ->leftJoin('admin_users as e', 'units.user_id', '=', 'e.id')
                ->orderBy('units.city','asc')
                ->orderBy('units.area','asc')
                ->orderBy('units.org_id','asc')
                ->orderBy('units.is_imp','asc')
                ->orderBy('units.type','asc')
                ->orderBy('units.xingzhi','asc')
                ->select('units.*', 'a.name as city', 'b.name as area', 'c.name as org_id', 'd.name as type', 'e.name as user_id')
                ->paginate($request->rows);
            $list = $res->items();
            foreach ($list as $k=>$v){
                foreach ($list as $k=>$v){
                    if($v->is_imp == 1){
                        $list[$k]->is_imp = '省级重点单位';
                    }elseif($v->is_imp == 2){
                        $list[$k]->is_imp = '市级重点单位';
                    }elseif($v->is_imp == 3){
                        $list[$k]->is_imp = '县区级重点单位';
                    }else{
                        $list[$k]->is_imp = '一般单位';
                    }
                }
            }
            $result['total'] = $res->total();
            $result['rows'] = $list;
            echo json_encode($result);
        }else{
            //类型
            $type = db::table('unit_type')->where('is_del','0')->get();
            $city = db::table('area')->where('dengji','=','1')->orderby('id')->get();//市
            return view('admin.units.closed', ['type'=>$type,'city' => $city]);
        }

        /*$map['is_del'] = '1';
        $res = DB::table('units')->where($map)->orderby('id','desc')->paginate(10);
        $count = DB::table('units')->where($map)->count();
        return view('admin.units.closed', ['res' => $res,'count'=>$count]);*/
    }

    public function jiancha($id,request $request)
    {
        if($request->rows) {
            $map['unit_id'] = $id;
            $res = DB::table('jiancha')->where($map)->paginate($request->rows);
            $list = $res->items();
            foreach ($list as $k=>$v){
                $list[$k]->user = getValue($v->user_id,'admin_users' ,'id' , 'name');
                if($v->status == 1){
                    $list[$k]->status = '合格';
                }elseif($v->status == 2){
                    $list[$k]->status = '不合格';
                }
            }
            $result['total'] = $res->total();
            $result['rows'] = $list;
            echo json_encode($result);
        }else{
            return view('admin.units.jiancha', ['id'=>$id]);
        }
    }

    public function add_jiancha(Request $request,$id)
    {
        $type = db::table('units')->where('id',$id)->value('type');
        if($request->isMethod('post')){
            if(!Input::get('status')){
                return ['status'=>0,'msg'=>'请选择检查结果'];
                //echo "<script>alert('请选择检查结果');window.history.go(-1);</script>";
                //exit();
            }
            $data = Input::except('_token');
            $data['unit_id'] = $id;
            $data['user_id'] = Auth::guard('admin')->user()->id;
            $data['type'] = $type;
            $data['created_at'] = date('Y-m-d H:i:s',time());
            $data['updated_at'] = date('Y-m-d H:i:s',time());

            $jiancha_id = DB::table('jiancha')->insertGetId($data);
            if($jiancha_id){
                return ['status'=>1,'msg'=>'添加成功'];
            }else{
                return ['status'=>0,'msg'=>'添加失败'];
            }
            /*if(Input::get('status') == 2){
                return redirect('admin/case/create/'.$jiancha_id);
            }*/
            //return redirect('admin/units/jiancha/'.$id);
        }
        return view('admin.units.add_jiancha',['type' => $type,'id'=>$id]);
    }

    public function edit_jiancha($id,Request $request)
    {
        if($request->isMethod('post')){
            $data = Input::except('_token');
            $data['user_id'] = Auth::guard('admin')->user()->id;
            $data['updated_at'] = date('Y-m-d H:i:s',time());
            $re = DB::table('jiancha')->where('id',$id)->update($data);
            if($re){
                return ['status'=>1,'msg'=>'编辑成功'];
            }else{
                return ['status'=>0,'msg'=>'编辑失败'];
            }
            //$cid = DB::table('jiancha')->where('id',$id)->value('unit_id');
            //return redirect('admin/units/jiancha/'.$cid);
        }
        $re = DB::table('jiancha')->where('id',$id)->first();
        $type = db::table('jiancha')->where('id',$id)->value('type');
        return view('admin.units.edit_jiancha', ['re' => $re,'type' => $type,'id'=>$id]);
    }

    public function del_jiancha($id)
    {
        $re = db::table('jiancha')->where('id',$id)->delete();
        if($re){
            return ['status'=> 1,'msg'=>'删除成功'];
        }else{
            return ['status'=> 0,'msg'=>'删除失败'];
        }
        //return redirect()->back();
    }

    public function zhidu($id,request $request)
    {
        if($request->rows) {
            $map['unit_id'] = $id;
            $res = DB::table('zhidu')->where($map)->paginate($request->rows);
            $list = $res->items();
            $result['total'] = $res->total();
            $result['rows'] = $list;
            echo json_encode($result);
        }else{
            return view('admin.units.zhidu', ['id'=>$id]);
        }
    }

    public function add_zhidu(Request $request,$id)
    {
        if($request->isMethod('post')){
            $data = Input::except('_token');
            $data['unit_id'] = $id;
            $data['user_id'] = Auth::guard('admin')->user()->id;
            $data['created_at'] = date('Y-m-d H:i:s',time());
            $data['updated_at'] = date('Y-m-d H:i:s',time());

            $re = DB::table('zhidu')->insert($data);
            if($re){
                return ['msg'=>'添加成功'];
            }else{
                return ['msg'=>'添加失败'];
            }
            //return redirect('admin/units/zhidu/'.$id);
        }
        return view('admin.units.add_zhidu', ['id'=>$id]);
    }

    public function edit_zhidu($id,Request $request)
    {
        if($request->isMethod('post')){
            $data = Input::except('_token');
            $data['user_id'] = Auth::guard('admin')->user()->id;
            $data['updated_at'] = date('Y-m-d H:i:s',time());
            $re = DB::table('zhidu')->where('id',$id)->update($data);
            if($re){
                return ['msg'=>'编辑成功'];
            }else{
                return ['msg'=>'编辑失败'];
            }
            //$cid = DB::table('zhidu')->where('id',$id)->value('unit_id');
            //return redirect('admin/units/zhidu/'.$cid);
        }
        $re = DB::table('zhidu')->where('id',$id)->first();
        return view('admin.units.edit_zhidu', ['re' => $re,'id'=>$id]);
    }

    public function del_zhidu($id)
    {
        $re = db::table('zhidu')->where('id',$id)->delete();
        if($re){
            return ['status'=> 1,'msg'=>'删除成功'];
        }else{
            return ['status'=> 0,'msg'=>'删除失败'];
        }
        //return redirect()->back();
    }

    public function type()
    {
        $res = DB::table('unit_type')->paginate(10);
        $count = DB::table('unit_type')->count();

        return view('admin.units.type', ['res' => $res,'count'=>$count]);
    }

    public function add_type(Request $request)
    {
        if($request->isMethod('post')){
            $data = Input::except('_token');
            $data['user_id'] = Auth::guard('admin')->user()->id;
            $data['created_at'] = date('Y-m-d H:i:s',time());
            $data['updated_at'] = date('Y-m-d H:i:s',time());

            DB::table('unit_type')->insert($data);
            return redirect('admin/units/type');
        }
        return view('admin.units.add_type');
    }

    public function edit_type($id,Request $request)
    {

        if($request->isMethod('post')){
            $this->validate($request, [
                'name' => 'required',
            ],$this->messages('type'));
            $data = Input::except('_token');
            $data['user_id'] = Auth::guard('admin')->user()->id;
            $data['updated_at'] = date('Y-m-d H:i:s',time());

            DB::table('unit_type')->where('id',$id)->update($data);
            return redirect('admin/units/type');
        }
        $re = DB::table('unit_type')->where('id',$id)->first();
        return view('admin.units.edit_type', ['re' => $re]);
    }

    public function del_type($id)
    {
        db::table('unit_type')->where('id',$id)->delete();
        return redirect()->back();
    }

    public function tuceng(request $request)
    {
        return view('admin.units.tuceng');
    }

    public function get_units(request $request)
    {
        $info = db::table('units')->where('is_imp','40')->get();
        $lastinfo = array();
        foreach ($info as $key => $value) {
            $img = '<img src="http://nb.yntosite.com/'.$value->pic.'" id="imgDemo" style="float: right;height: 100;position: absolute;right: 0;bottom: 0;">';
            $url = "<br><br><a href='javascript:;' onclick='go($value->id);' style='color: red'>点击查看</a>";
            if ($value->is_imp == 40) {
                $type = "<br>是否为重点单位: <p style='color:red;display: inline-block;'>否</p>";
            }else{
                $type = "<br>是否为重点单位: <p style='color:green;display: inline-block;'>是</p>";
            }
            $faren = '<br>法人名称：'.$value->frdb_name;
            $lianxi = '<br>联系方式：'.$value->frdb_phone;
            $laststr = '<br>公司名：'.$value->name.$faren. $lianxi.$type.$url.$img;
            $lastinfo[$key][0] = $value->jd;
            $lastinfo[$key][1] = $value->wd;
            $lastinfo[$key][2] = $laststr;
            //$lastinfo[$key][3] = $value->map_pic;
            $lastinfo[$key][4] = $value->is_imp;
            //$lastinfo[$key][5] = 'http://nb.yntosite.com/'.$value->thumb_pic;
        }
        return $lastinfo;
    }

    public function get_zdunits(request $request)
    {

        $info = db::table('units')->whereIn('is_imp', [10, 20, 30])->get();
        $lastinfo = array();
        foreach ($info as $key => $value) {
            $img = '<img src="http://nb.yntosite.com/'.$value->pic.'" id="imgDemo" style="float: right;height: 100;position: absolute;right: 0;bottom: 0;">';
            $url = "<br><br><a href='javascript:;' onclick='go($value->id);' style='color: red'>点击查看</a>";
            if ($value->is_imp == 40) {
                $type = "<br>是否为重点单位: <p style='color:red;display: inline-block;'>否</p>";
            }else{
                $type = "<br>是否为重点单位: <p style='color:green;display: inline-block;'>是</p>";
            }
            $faren = '<br>法人名称：'.$value->frdb_name;
            $lianxi = '<br>联系方式：'.$value->frdb_phone;
            $laststr = '<br>公司名：'.$value->name.$faren. $lianxi.$type.$url.$img;
            $lastinfo[$key][0] = $value->jd;
            $lastinfo[$key][1] = $value->wd;
            $lastinfo[$key][2] = $laststr;
            $lastinfo[$key][3] = 'http://nb.yntosite.com/'.$value->pic;
            $lastinfo[$key][4] = $value->is_imp;
            $lastinfo[$key][5] = 'http://nb.yntosite.com/images/zhongdian.png';
        }
        return $lastinfo;
    }

    public function get_units_list(request $request)
    {

        $area = DB::table('area')->where('id',$request->id)->first();
        if($area->dengji == 1){
            $info = db::table('units')->where('city',$request->id)->whereIn('is_imp', [10, 20, 30])->get();
            $lastinfo = array();
            foreach ($info as $key => $value) {
                $img = '<img src="http://nb.yntosite.com/'.$value->pic.'" id="imgDemo" style="float: right;height: 100;position: absolute;right: 0;bottom: 0;">';
                $url = "<br><br><a href='javascript:;' onclick='go($value->id);' style='color: red'>点击查看</a>";
                if ($value->is_imp == 40) {
                    $type = "<br>是否为重点单位: <p style='color:red;display: inline-block;'>否</p>";
                }else{
                    $type = "<br>是否为重点单位: <p style='color:green;display: inline-block;'>是</p>";
                }
                $faren = '<br>法人名称：'.$value->frdb_name;
                $lianxi = '<br>联系方式：'.$value->frdb_phone;
                $laststr = '<br>公司名：'.$value->name.$faren. $lianxi.$type.$url.$img;
                $lastinfo[$key][0] = $value->jd;
                $lastinfo[$key][1] = $value->wd;
                $lastinfo[$key][2] = $laststr;
                $lastinfo[$key][3] = 'http://nb.yntosite.com/'.$value->pic;
                $lastinfo[$key][4] = $value->is_imp;
                $lastinfo[$key][5] = 'http://nb.yntosite.com/images/zhongdian.png';
            }

            $info2 = db::table('units')->where('city',$request->id)->where('is_imp','40')->get();
            $lastinfo2 = array();
            foreach ($info2 as $key => $value) {
                $img = '<img src="http://nb.yntosite.com/'.$value->pic.'" id="imgDemo" style="float: right;height: 100;position: absolute;right: 0;bottom: 0;">';
                $url = "<br><br><a href='javascript:;' onclick='go($value->id);' style='color: red'>点击查看</a>";
                if ($value->is_imp == 40) {
                    $type = "<br>是否为重点单位: <p style='color:red;display: inline-block;'>否</p>";
                }else{
                    $type = "<br>是否为重点单位: <p style='color:green;display: inline-block;'>是</p>";
                }
                $faren = '<br>法人名称：'.$value->frdb_name;
                $lianxi = '<br>联系方式：'.$value->frdb_phone;
                $laststr = '<br>公司名：'.$value->name.$faren. $lianxi.$type.$url.$img;
                $lastinfo2[$key][0] = $value->jd;
                $lastinfo2[$key][1] = $value->wd;
                $lastinfo2[$key][2] = $laststr;
                //$lastinfo[$key][3] = $value->map_pic;
                $lastinfo2[$key][4] = $value->is_imp;
                //$lastinfo[$key][5] = 'http://nb.yntosite.com/'.$value->thumb_pic;
            }
        }

        if($area->dengji == 2){
            $info = db::table('units')->where('area',$request->id)->whereIn('is_imp', [10, 20, 30])->get();
            $lastinfo = array();
            foreach ($info as $key => $value) {
                $img = '<img src="http://nb.yntosite.com/'.$value->pic.'" id="imgDemo" style="float: right;height: 100;position: absolute;right: 0;bottom: 0;">';
                $url = "<br><br><a href='javascript:;' onclick='go($value->id);' style='color: red'>点击查看</a>";
                if ($value->is_imp == 40) {
                    $type = "<br>是否为重点单位: <p style='color:red;display: inline-block;'>否</p>";
                }else{
                    $type = "<br>是否为重点单位: <p style='color:green;display: inline-block;'>是</p>";
                }
                $faren = '<br>法人名称：'.$value->frdb_name;
                $lianxi = '<br>联系方式：'.$value->frdb_phone;
                $laststr = '<br>公司名：'.$value->name.$faren. $lianxi.$type.$url.$img;
                $lastinfo[$key][0] = $value->jd;
                $lastinfo[$key][1] = $value->wd;
                $lastinfo[$key][2] = $laststr;
                $lastinfo[$key][3] = 'http://nb.yntosite.com/'.$value->pic;
                $lastinfo[$key][4] = $value->is_imp;
                $lastinfo[$key][5] = 'http://nb.yntosite.com/images/zhongdian.png';
            }

            $info2 = db::table('units')->where('area',$request->id)->where('is_imp','40')->get();
            $lastinfo2 = array();
            foreach ($info2 as $key => $value) {
                $img = '<img src="http://nb.yntosite.com/'.$value->pic.'" id="imgDemo" style="float: right;height: 100;position: absolute;right: 0;bottom: 0;">';
                $url = "<br><br><a href='javascript:;' onclick='go($value->id);' style='color: red'>点击查看</a>";
                if ($value->is_imp == 40) {
                    $type = "<br>是否为重点单位: <p style='color:red;display: inline-block;'>否</p>";
                }else{
                    $type = "<br>是否为重点单位: <p style='color:green;display: inline-block;'>是</p>";
                }
                $faren = '<br>法人名称：'.$value->frdb_name;
                $lianxi = '<br>联系方式：'.$value->frdb_phone;
                $laststr = '<br>公司名：'.$value->name.$faren. $lianxi.$type.$url.$img;
                $lastinfo2[$key][0] = $value->jd;
                $lastinfo2[$key][1] = $value->wd;
                $lastinfo2[$key][2] = $laststr;
                //$lastinfo[$key][3] = $value->map_pic;
                $lastinfo2[$key][4] = $value->is_imp;
                //$lastinfo[$key][5] = 'http://nb.yntosite.com/'.$value->thumb_pic;
            }
        }


        return ['zhongdian'=>$lastinfo,'feizhongdian'=>$lastinfo2];
    }

    public function cxzd(){

        $info = db::table('units')->get();
        $lastinfo = array();
        foreach ($info as $key => $value) {
            $url = "<br><a href='".url('admin/units/view',$value->id)."' width='100%'style='color: red'>点击查看</a>";
            if ($value->is_imp == 1) {
                $type = "<br>是否为重点单位: 是";
            }else{
                $type = "<br>是否为重点单位: 否";
            }

            $laststr = '公司名：'.$value->name.$type.$url;
            $lastinfo[$key][0] = $value->jd;
            $lastinfo[$key][1] = $value->wd;
            $lastinfo[$key][2] = $laststr;
            $lastinfo[$key][3] = $value->map_pic;
            $lastinfo[$key][4] = $value->is_imp;
        }

         $result = array('message' =>  $lastinfo);
         echo json_encode($result,TRUE);
         exit();
    }

    public function xinxi($id,request $request)
    {
        if($request->rows) {
            $map['unit_id'] = $id;
            $res = DB::table('xinxi')->where($map)->paginate($request->rows);
            $list = $res->items();
            foreach ($list as $k=>$v){
                if($list[$k]->is_read == 0){
                    $list[$k]->is_read = '未读';
                }
                if($list[$k]->is_read == 1){
                    $list[$k]->is_read = '已读';
                }
                if($list[$k]->type == 1){
                    $list[$k]->type = '接收';
                }
                if($list[$k]->type == 2){
                    $list[$k]->type = '发出';
                }

                $list[$k]->user = getValue($list[$k]->user_id,'admin_users' ,'id' ,'name' );
            }
            $result['total'] = $res->total();
            $result['rows'] = $list;
            echo json_encode($result);
        }else{
            return view('admin.units.xinxi', ['id'=>$id]);
        }
    }

    public function add_xinxi(Request $request,$id)
    {
        if($request->isMethod('post')){
            $data = Input::except('_token');
            $data['unit_id'] = $id;
            $data['user_id'] = Auth::guard('admin')->user()->id;
            $data['created_at'] = date('Y-m-d H:i:s',time());
            $data['updated_at'] = date('Y-m-d H:i:s',time());
            $re = DB::table('xinxi')->insert($data);
            if($re){
                return ['msg'=>'添加成功'];
            }else{
                return ['msg'=>'添加失败'];
            }
            //return redirect('admin/units/zhidu/'.$id);
        }
        $units_name = db::table('units')->where('id',$id)->value('name');
        //发送人
        $user = db::table('admin_users')->whereIn('dengji', [1, 2, 3 ,4])->get();
        return view('admin.units.add_xinxi', ['id'=>$id,'units_name'=>$units_name,'user'=>$user]);
    }

    public function edit_xinxi($id,Request $request)
    {
        if($request->isMethod('post')){
            $data = Input::except('_token');
            $data['user_id'] = Auth::guard('admin')->user()->id;
            $data['updated_at'] = date('Y-m-d H:i:s',time());
            $re = DB::table('xinxi')->where('id',$id)->update($data);
            if($re){
                return ['msg'=>'编辑成功'];
            }else{
                return ['msg'=>'编辑失败'];
            }
            //$cid = DB::table('zhidu')->where('id',$id)->value('unit_id');
            //return redirect('admin/units/zhidu/'.$cid);
        }
        $re = DB::table('xinxi')->where('id',$id)->first();
        //如果是企业接收到的信息
        if($re->unit_id == Auth::guard('admin')->user()->unit_id && $re->type == 1){
            $status['is_read'] = '1';
            DB::table('xinxi')->where('id',$id)->update($status);
        }
        //如果是企业发出的信息
        if($re->to_user == Auth::guard('admin')->user()->id && $re->type == 2){
            $status['is_read'] = '1';
            DB::table('xinxi')->where('id',$id)->update($status);
        }
        $units_name = db::table('units')->where('id',$re->unit_id)->value('name');
        //发送人
        $user = db::table('admin_users')->whereIn('dengji', [1, 2, 3 ,4])->get();
        return view('admin.units.edit_xinxi', ['re' => $re,'id'=>$id,'units_name'=>$units_name,'user'=>$user]);
    }

    public function del_xinxi($id)
    {
        $re = db::table('xinxi')->where('id',$id)->delete();
        if($re){
            return ['status'=> 1,'msg'=>'删除成功'];
        }else{
            return ['status'=> 0,'msg'=>'删除失败'];
        }
        //return redirect()->back();
    }

    public function getjwd(){
        return view('admin.units.getjwd');
    }

    //打印单位
    public function print_unit($id)
    {
        $re = DB::table('units')->where('id',$id)->first();
        //要害部位数量
        $count['buwei_num'] = db::table('zhongyaobuwei')->where('unit_id',$id)->count();
        //重点岗位人数
        $count['zdgw_num'] = db::table('zhongdianrenyuan')->where('unit_id',$id)->count();
        //在职保卫人数
        $count['zaizhi_num'] = db::table('baoweirenyuan')->where('unit_id',$id)->where('guanxi','在职职工')->count();
        //自招保安人数
        $count['zizhao_num'] = db::table('baoweirenyuan')->where('unit_id',$id)->where('guanxi','自招')->count();
        //外聘保安人数
        $count['waipin_num'] = db::table('baoweirenyuan')->where('unit_id',$id)->where('guanxi','外聘')->count();
        //民警
        $police = db::table('admin_users')->where('org_id',$re->org_id)->get();
        //行业字段
        $field_count = db::table('unit_type_field')->where('type_id',$re->type)->where('is_show','1')->count();
        if($field_count > 0){
            for ($x=0; $x*2<$field_count; $x++) {
                $field[$x] = db::table('unit_type_field')->where('type_id',$re->type)->where('is_show','1')->orderby('paixu','asc')->offset(2*$x)->limit(2)->get();
            }
            foreach ($field as $k=>$v){
                foreach ($v as $k2=>$v2){
                    if($v2->field_type == 2){
                        $v2->daan = db::table('unit_type_field_daan')->where('pid',$v2->id)->get();
                    }
                }
            }
        }else{
            $field = array();
        }

        return view('admin.units.print_unit', ['re' => $re,'id' => $id,'count'=>$count,'police'=>$police,'field'=>$field]);
    }

    //打印重点人员
    public function print_faren($id)
    {
        $re = DB::table('faren')->where('id',$id)->first();
        $nation = db::table('nation')->get();
        return view('admin.units.print_faren', ['re' => $re,'nation' => $nation,'id'=>$id]);
    }

    //打印保卫人员
    public function print_baowei($id)
    {
        $re = DB::table('baoweirenyuan')->where('id',$id)->first();
        $nation = db::table('nation')->get();
        return view('admin.units.print_baowei', ['re' => $re,'nation' => $nation,'id'=>$id]);
    }

    //打印重点人员
    public function print_zdry($id)
    {
        $re = DB::table('zhongdianrenyuan')->where('id',$id)->first();
        $nation = db::table('nation')->get();
        return view('admin.units.print_zdry', ['re' => $re,'nation' => $nation,'id'=>$id]);
    }

    //打印重点人员
    public function print_zybw($id)
    {
        $re = DB::table('zhongyaobuwei')->where('id',$id)->first();
        $nation = db::table('nation')->get();
        return view('admin.units.print_zybw', ['re' => $re,'nation' => $nation,'id'=>$id]);
    }

    public function tab($id)
    {
        return view('admin.units.tab', ['id' => $id]);
    }

    public function tab2($id)
    {
        return view('admin.units.tab2', ['id' => $id]);
    }

    //导出
    public function export_unit(request $request){
        //如果是民警登录，只能查看自己辖区的单位
        if(Auth::guard('admin')->user()->org_id != null && Auth::guard('admin')->user()->dengji == 4){
            $map['units.org_id'] = Auth::guard('admin')->user()->org_id;
        }
        //如果是企业用户
        if(Auth::guard('admin')->user()->role_id == 22){
            $map['units.id'] = Auth::guard('admin')->user()->unit_id;
        }
        if($request->city != null){
            if($request->city == 0){

            }else{
                $map['units.city'] = $request->city;
            }
        }
        if($request->area){
            $map['units.area'] = $request->area;
        }
        if($request->org_id){
            $map['units.org_id'] = $request->org_id;
        }
        if($request->name){
            $map[] = array('units.name','like','%'.$request->name.'%');
        }
        if($request->frdb_name){
            $map['units.frdb_name'] = $request->frdb_name;
        }
        if($request->type){
            $map['units.type'] = $request->type;
        }
        if($request->input('jibie') != null){
            $map['is_imp'] = $request->jibie;
        }
        $map['units.is_del'] = '0';
        $res = DB::table('units')
            ->where($map)
            ->leftJoin('area as a', 'units.city', '=', 'a.id')
            ->leftJoin('area as b', 'units.area', '=', 'b.id')
            ->leftJoin('area as c', 'units.org_id', '=', 'c.id')
            ->leftJoin('unit_type as d', 'units.type', '=', 'd.id')
            ->leftJoin('admin_users as e', 'units.user_id', '=', 'e.id')
            ->select('units.*', 'a.name as city', 'b.name as area', 'c.name as org_id', 'd.name as type', 'e.name as user_id')
            ->get();

        $cellData[] = ['市','区','所属派出所','重点级别','企业类型','企业性质','单位名','详细地址','联系人','联系电话'];
        foreach ($res as $k=>$v){
            if($v->is_imp == 1){
                $v->is_imp = '省级';
            }elseif($v->is_imp == 2){
                $v->is_imp = '市级';
            }elseif($v->is_imp == 3){
                $v->is_imp = '县级';
            }elseif($v->is_imp == 4){
                $v->is_imp = '乡级';
            }else{
                $v->is_imp = '非重点';
            }
            $cellData[$k+1] = [$v->city,$v->area,$v->org_id,$v->is_imp,$v->type,$v->xingzhi,$v->name,$v->address,$v->frdb_name,$v->frdb_phone];
        }
        Excel::create('实有单位导出_'.date('YmdHis',time()),function($excel) use ($cellData){
            $excel->sheet('score', function($sheet) use ($cellData){
                $sheet->rows($cellData);
            });
        })->export('xls');
    }

    public function field($id)
    {
        $res = DB::table('unit_type_field')->where('type_id',$id)->orderby('paixu','asc')->paginate(10);
        $count = DB::table('unit_type_field')->where('type_id',$id)->count();

        return view('admin.units.field', ['res' => $res,'count'=>$count,'id'=>$id]);
    }

    public function add_field($id,Request $request)
    {
        if($request->isMethod('post')){
            $data = Input::except('_token','daan');
            $data['user_id'] = Auth::guard('admin')->user()->id;
            $data['type_id'] = $id;
            $data['created_at'] = date('Y-m-d H:i:s',time());
            $data['updated_at'] = date('Y-m-d H:i:s',time());

            $field_id = DB::table('unit_type_field')->insertGetId($data);

            //处理答案
            $daan = Input::get('daan');
            foreach ($daan as $k=>$v){
                if($v){
                    $arr['pid'] = $field_id;
                    $arr['val'] = $v;
                    DB::table('unit_type_field_daan')->insert($arr);
                }
            }
            return redirect('admin/units/field/'.$id);
        }
        return view('admin.units.add_field');
    }

    public function edit_field($id,Request $request)
    {
        $re = DB::table('unit_type_field')->where('id',$id)->first();
        if($request->isMethod('post')){
            $data = Input::except('_token','daan');
            $data['user_id'] = Auth::guard('admin')->user()->id;
            $data['updated_at'] = date('Y-m-d H:i:s',time());
            //处理答案
            $daan = Input::get('daan');
            //先删除原有答案
            DB::table('unit_type_field_daan')->where('pid',$id)->delete();
            //再添加新的答案
            foreach ($daan as $k=>$v){
                if($v){
                    $arr['pid'] = $id;
                    $arr['val'] = $v;
                    DB::table('unit_type_field_daan')->insert($arr);
                }
            }
            DB::table('unit_type_field')->where('id',$id)->update($data);
            $type_id = $re->type_id;
            return redirect('admin/units/field/'.$type_id);
        }

        //答案
        $res = DB::table('unit_type_field_daan')->where('pid',$id)->get();
        if(count($res) != 0){
            foreach ($res as $k=>$v){
                $list[$k+1] = $v->val;
            }
        }else{
            $list = array();
        }
        return view('admin.units.edit_field', ['list'=>$list,'re' => $re]);
    }

    public function del_field($id)
    {
        db::table('unit_type_field')->where('id',$id)->delete();
        db::table('unit_type_field_daan')->where('pid',$id)->delete();
        return redirect()->back();
    }

    //排序
    public function save_field_sort(){
        $where['id'] = $_POST['id'];
        $data['paixu'] = $_POST['paixu'];
        db::table('unit_type_field')->where($where)->update($data);
    }

    public function aqjc($id,$type)
    {
        $res = DB::table('unit_type_aqjc')->where('type_id',$id)->where('aqjc_type',$type)->orderby('paixu','asc')->paginate(10);
        $count = DB::table('unit_type_aqjc')->where('type_id',$id)->where('aqjc_type',$type)->count();

        return view('admin.units.aqjc', ['res' => $res,'count'=>$count,'id'=>$id,'type'=>$type]);
    }

    public function add_aqjc($id,$type,Request $request)
    {
        if($request->isMethod('post')){
            $data = Input::except('_token','daan');
            $data['user_id'] = Auth::guard('admin')->user()->id;
            $data['type_id'] = $id;
            $data['aqjc_type'] = $type;
            $data['created_at'] = date('Y-m-d H:i:s',time());
            $data['updated_at'] = date('Y-m-d H:i:s',time());

            $jiancha_id = DB::table('unit_type_aqjc')->insertGetId($data);
            //处理答案
            $daan = Input::get('daan');
            foreach ($daan as $k=>$v){
                if($v){
                    $arr['pid'] = $jiancha_id;
                    $arr['val'] = $v;
                    DB::table('unit_type_aqjc_daan')->insert($arr);
                }
            }
            return redirect('admin/units/aqjc/'.$id.'/'.$type);
        }
        return view('admin.units.add_aqjc',['type'=>$type]);
    }

    public function edit_aqjc($id,$type,Request $request)
    {
        $re = DB::table('unit_type_aqjc')->where('id',$id)->first();
        if($request->isMethod('post')){
            $data = Input::except('_token','daan');
            $data['user_id'] = Auth::guard('admin')->user()->id;
            $data['updated_at'] = date('Y-m-d H:i:s',time());
            //处理答案
            $daan = Input::get('daan');
            //先删除原有答案
            DB::table('unit_type_aqjc_daan')->where('pid',$id)->delete();
            //再添加新的答案
            foreach ($daan as $k=>$v){
                if($v){
                    $arr['pid'] = $id;
                    $arr['val'] = $v;
                    DB::table('unit_type_aqjc_daan')->insert($arr);
                }
            }
            DB::table('unit_type_aqjc')->where('id',$id)->update($data);
            $pid = $re->type_id;
            return redirect('admin/units/aqjc/'.$pid.'/'.$type);
        }
        //答案
        $res = DB::table('unit_type_aqjc_daan')->where('pid',$id)->get();
        if(count($res) != 0){
            foreach ($res as $k=>$v){
                $list[$k+1] = $v->val;
            }
        }else{
            $list = array();
        }

        return view('admin.units.edit_aqjc', ['list'=>$list,'re' => $re,'type'=>$type]);
    }

    //排序
    public function save_aqjc_sort(){
        $where['id'] = $_POST['id'];
        $data['paixu'] = $_POST['paixu'];
        db::table('unit_type_aqjc')->where($where)->update($data);
    }

    public function del_aqjc($id)
    {
        db::table('unit_type_aqjc')->where('id',$id)->delete();
        db::table('unit_type_aqjc_daan')->where('pid',$id)->delete();
        return redirect()->back();
    }

    public function update($id)
    {
        $status = db::table('units')->where('id',$id)->value('status');
        if($status == 1){
            echo "<script>alert('未编辑信息，无需更新!');window.history.go(-1);</script>";
            exit();
        }elseif($status == 5){
            $data['status'] = '10';
            $re = db::table('units')->where('id',$id)->update($data);
        }else{
            echo "<script>alert('已提交更新请求，正在审核中!');window.history.go(-1);</script>";
            exit();
        }
        return redirect()->back();
    }



}
